package cn.sunxinao.pm;

import cn.sunxinao.pm.services.AbstractPetManager;
import cn.sunxinao.pm.services.AbstractUserManager;
import java.util.Scanner;

/**
 * 程序入口抽象类
 */
public abstract class StartUp {
    /**
     * 宠物管理服务
     */
    protected final AbstractPetManager petManager;

    /**
     * 用户相关服务
     */
    protected final AbstractUserManager userManager;

    protected StartUp(AbstractPetManager petManager, AbstractUserManager userManager) {
        this.petManager = petManager;
        this.userManager = userManager;
    }

    protected void run() {
        final Scanner kb = new Scanner(System.in);

        try { // 捕获键盘输入的异常
            if (!userManager.login(kb)) {
                System.err.println("登陆失败，退出");
                return;
            }

            System.out.println("\n\n欢迎使用Pet's Home Adoption -- Pets Manager Cli(宠物之家收养所宠物管理系统控制台)");
            System.out.println("输入 help 查看命令列表\n\n");

            for (String cmdStr = "help"; !cmdStr.startsWith("exit"); cmdStr = kb.nextLine().trim()) {
                if (cmdStr.isEmpty()) continue;
                switch (cmdStr) {
                    case "help": {
                        System.out.println("宠物管理:" +
                            "\n\tlist: 列出所有宠物" +
                            "\n\tadd: 收养新宠物" +
                            "\n\tdel: 删除宠物" +
                            "\n\tfind: 查找宠物" +
                            "\n\tupdate: 更新宠物信息" +
                            "\n\tadopt: 领养宠物" +
                            "\n管理员账号管理:" +
                            "\n\tchpwd: 修改密码" +
                            "\n\tunreg: 删除账号" +
                            "\n\texit: 退出");
                    }
                    break;
                    case "add": {
                        petManager.addPet(kb);
                    }
                    break;
                    case "list": {
                        boolean success = petManager.list();
                        if (!success) System.err.println("没有数据");
                    }
                    break;
                    case "del": {
                        boolean success = petManager.delete(kb);
                        System.out.println(success ? "操作成功" : "找不到该宠物");
                    }
                    break;
                    case "find": {
                        boolean success = petManager.find(kb);
                        System.out.println(success ? "查询结束" : "找不到该宠物(ノ｀Д)ノ");
                    }
                    break;
                    case "update": {
                        boolean success = petManager.update(kb);
                        System.out.println(success ? "操作成功" : "找不到该宠物");
                    }
                    break;
                    case "adopt": {
                        boolean success = petManager.adopt(kb);
                        System.out.println(success ? "操作成功" : "找不到该宠物");
                    }
                    break;
                    case "chpwd": {
                        System.out.println("您要为哪个账号更改密码？[输入用户名]");
                        System.out.println(userManager.changePassword(kb.nextLine().trim(), kb) ? "操作成功" : "操作失败");
                    }
                    break;
                    case "unreg": {
                        System.out.println("您要删除哪个账号？[输入用户名]");
                        System.out.println(userManager.unRegister(kb.nextLine().trim(), kb) ? "操作成功" : "操作失败");
                    }
                    break;
                    default: {
                        System.err.println("未知指令，输入 help 查看命令列表");
                    }
                    break;
                }
                if (!petManager.save() || !userManager.save())
                    System.err.println("提示: 无法保存文件，数据可能会丢失");
            }
        } catch (Exception e) {
            // 如果键盘输入了不合法的字符可能会导致程序终止
            System.err.println("Terminating...");
        }

        // 退出前保存一下
        if (!petManager.save() || !userManager.save())
            System.err.println("提示: 无法保存文件，数据可能会丢失");
    }
}
